package com.wjw.controller;

import com.wjw.entity.Collect;
import com.wjw.entity.Product;
import com.wjw.service.ImageUpload;
import com.wjw.service.ProductService;
import com.wjw.vo.ProductVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author WuJianWei
 * @date 2022/4/24 13:58
 */
@RestController
@RequestMapping("/product-upload")
public class ProductUpload {
    @Autowired
    private ImageUpload imageUpload;
    @Autowired
    private ProductService productService;

    @RequestMapping(value = "/image-upload")
    @ResponseBody
    public Map imageUpload( MultipartFile file){
        HashMap map = new HashMap<String, String>();
        String img = imageUpload.img(file);
        if (img.length() < 0){
            map.put("code", "004");
            map.put("msg", "图片上传失败");
        }
        map.put("code", "001");
        map.put("msg", "图片上传成功");
        map.put("url",img);
        return map;
    }

    @RequestMapping(value = "/product-save")
    @ResponseBody
    public Map productSave(String productName,String productTitle,String productIntro,String imgUrl,long businessId){
        HashMap map = new HashMap<String, String>();
        Product productImg = new Product();
        productImg.setProductName(productName);
        productImg.setProductTitle(productTitle);
        productImg.setProductIntro(productIntro);
        productImg.setProductPicture(imgUrl);
        productImg.setBusinessId(String.valueOf(businessId));
        Boolean add = productService.add(productImg);
        if (add){
            map.put("code", "001");
            map.put("msg", "产品上传成功");
        }else {
            map.put("code", "004");
            map.put("msg", "产品上传失败");
        }
        return map;
    }


    @RequestMapping(value = "/get-product")
    @ResponseBody
    public Map getProduct(@RequestBody Map<String, String> paramMap){
        Long user_id = Long.valueOf(paramMap.get("user_id"));
        HashMap<String, Object> map = new HashMap<>();
        List<Product> product = productService.getProduct(user_id);
       if (product.size() > 0){
           List<Collect> collectList = new ArrayList<>();
           for (Product product1 : product) {
               Collect collect = new Collect();
               collect.setProductName(product1.getProductName());
               collect.setProductPicture(product1.getProductPicture());
               collect.setProductPrice(product1.getProductPrice());
               collect.setSellingPrice(product1.getProductSellingPrice());
               collect.setTitle(product1.getProductTitle());
               collectList.add(collect);
           }
           map.put("collectList", collectList);
           map.put("code", "001");
           return map;
       }
        map.put("code", "002");
        map.put("msg", "该商家没有上传产品");
       return null;
    }

    @RequestMapping(value = "/delete")
    @ResponseBody
    public Map delete(@RequestBody Integer productId ){
        HashMap map = new HashMap<String, String>();
        int delete = productService.delete(productId);
        if (delete >0){
            map.put("code", "001");
            map.put("msg", "删除成功");
        }else  {
            map.put("code", "004");
            map.put("msg", "删除失败失败");
        }
        return map;
    }


}
